﻿using System;
using System.IO;
using System.Text;

namespace JianZhangTool.config
{
    public static class StaticConfig
    {
        /// <summary>
        /// 上次输入的原始房间号
        /// </summary>
        public static int last_origin_roomId;
        /// <summary>
        /// 是否自动连接
        /// </summary>
        public static int auto_connect;
        /// <summary>
        /// 舰长深渊次数参数
        /// </summary>
        public static int jianzhang_shenyuan_param;
        /// <summary>
        /// 提督深渊次数参数
        /// </summary>
        public static int tidu_shenyuan_param;
        /// <summary>
        /// 总督深渊次数参数
        /// </summary>
        public static int zongdu_shenyuan_param;

        public static void Load()
        {
            try
            {
                string full_filename = Environment.CurrentDirectory + @"\conf\static_config.properties";
                if (File.Exists(full_filename))
                {
                    StreamReader sr = new StreamReader(full_filename);
                    if (sr != null)
                    {
                        try
                        {
                            while (sr.Peek() >= 0)
                            {
                                string line_str = sr.ReadLine();
                                string[] splits = line_str.Split('=');
                                if (splits.Length == 2)
                                {
                                    try
                                    {
                                        switch (splits[0])
                                        {
                                            case "last_origin_roomId":
                                                {
                                                    last_origin_roomId = int.Parse(splits[1]);
                                                    break;
                                                }
                                            case "auto_connect":
                                                {
                                                    auto_connect = int.Parse(splits[1]);
                                                    break;
                                                }
                                            case "jianzhang_shenyuan_param":
                                                {
                                                    jianzhang_shenyuan_param = int.Parse(splits[1]);
                                                    break;
                                                }
                                            case "tidu_shenyuan_param":
                                                {
                                                    tidu_shenyuan_param = int.Parse(splits[1]);
                                                    break;
                                                }
                                            case "zongdu_shenyuan_param":
                                                {
                                                    zongdu_shenyuan_param = int.Parse(splits[1]);
                                                    break;
                                                }
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        // 异常不中断读取
                                    }
                                }
                            }
                        }
                        finally
                        {
                            sr.Close();
                        }
                    }
                }
            }
            catch (Exception)
            {
                // 理论上 只有目标文件被占用的情况下会跑到这里
                Main.Instance.add_log("[常量加载] 出错! 请检查常量文件是否占用中");
            }
        }

        public static void Save()
        {
            string file_path = Environment.CurrentDirectory + @"\conf\";
            if (!Directory.Exists(file_path))
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(file_path);
                directoryInfo.Create();
            }
            StreamWriter sw = new StreamWriter(file_path + "static_config.properties", false, Encoding.UTF8);
            sw.WriteLine("last_origin_roomId=" + last_origin_roomId);
            sw.WriteLine("auto_connect=" + auto_connect);
            sw.WriteLine("jianzhang_shenyuan_param=" + jianzhang_shenyuan_param);
            sw.WriteLine("tidu_shenyuan_param=" + tidu_shenyuan_param);
            sw.WriteLine("zongdu_shenyuan_param=" + zongdu_shenyuan_param);
            sw.Flush();
            sw.Close();
        }
    }
}
